<?php
/**
 * Date: 2024.8
 * Author: kahn
 * jwt验证
 */
use config\Response;
use config\Route;
use plugins\Plugin;
use service\uc\JwtService;

class JwtPlugin extends Yaf_Plugin_Abstract
{
    use Plugin;

    public function routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {

        $route = $this->getRoute();
        $method = $this->getMethod();

        $userId = 0;
        $jump = Route::jwtType($method, $route);

        if($jump != Route::JWT_JUMP){ //如果路由地址不可跳过JWT验证
            $ret = JwtService::verifyJwt($userId, $jwt);
            if($ret == Response::JWT_ERROR && $jump == Route::JWT_REQUIRED){ //JWT验证错误且必须要JWT验证
                response($ret);
            }elseif($ret == Response::JWT_EXPIRE){
                response($ret, ['token' => $jwt]);
            }elseif($ret == Response::SYSTEM_ERROR){
                response(Response::SYSTEM_ERROR);
            }
        }

        Yaf_Registry::set('userId', $userId);
    }


}